package com.abc.robot01.fragment

import android.app.Activity
import android.content.Context
import android.net.Uri
import android.os.Bundle
import android.util.Log
import androidx.fragment.app.Fragment
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.webkit.WebSettings
import android.webkit.WebView
import android.webkit.WebViewClient
import android.widget.FrameLayout
import android.widget.ImageView
import androidx.compose.foundation.background
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.size
import androidx.compose.foundation.shape.CircleShape
import androidx.compose.material3.Icon
import androidx.compose.material3.IconButton
import androidx.compose.material3.Slider
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.rememberCoroutineScope
import androidx.compose.runtime.setValue
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.platform.ComposeView
import androidx.compose.ui.res.painterResource
import androidx.compose.ui.unit.dp
import androidx.compose.ui.viewinterop.AndroidView
import androidx.media3.common.MediaItem
import androidx.media3.common.Player
import androidx.media3.exoplayer.ExoPlayer
import androidx.media3.ui.PlayerView
import com.abc.robot01.R
import com.abc.robot01.databinding.FragmentFirstBinding
import com.bumptech.glide.Glide
import com.bumptech.glide.load.engine.DiskCacheStrategy
import kotlinx.coroutines.delay
import kotlinx.coroutines.launch


/**
 */
class H5Fragment(private val fileUrl: String) : Fragment() {
    val TAG="H5Fragment"

    private lateinit var webView: WebView

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        Log.e(TAG,"onCreate");
    }

    override fun onCreateView(
        inflater: LayoutInflater, container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View {
        val view = inflater.inflate(R.layout.fragment_h5, container, false)
//         webView = view.findViewById<WebView>(R.id.webView)
//
//        // Setup WebView settings
//        webView.settings.javaScriptEnabled = true
//        webView.settings.cacheMode = WebSettings.LOAD_DEFAULT
//        webView.settings.domStorageEnabled = true
//        // Ensure the WebView opens URLs in the WebView itself
//        webView.webViewClient = WebViewClient()
//            webView.loadUrl(fileUrl)
        return view
    }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
    }

    override fun onResume() {
        super.onResume()
        // 创建 WebView
        webView = WebView(requireContext()).apply {
            layoutParams = FrameLayout.LayoutParams(
                ViewGroup.LayoutParams.MATCH_PARENT,
                ViewGroup.LayoutParams.MATCH_PARENT
            )
            clearCache(true)
            settings.javaScriptEnabled = true
            settings.cacheMode = WebSettings.LOAD_NO_CACHE
            settings.domStorageEnabled = true
            webViewClient = WebViewClient()
        }

        // 获取根布局并添加 WebView
        (view as? FrameLayout)?.addView(webView)
        webView.loadUrl(fileUrl)
        Log.e(TAG,"onResume");
    }

    override fun onStart() {
        super.onStart()
        Log.e(TAG,"onStart");
    }

    override fun onPause() {
        super.onPause()
        // 暂停WebView中的音频播放
        webView.onPause()
        webView.destroy()
        Log.e(TAG,"onPause");
    }

    override fun onStop() {
        super.onStop()
        Log.e(TAG,"onStop");
    }

    override fun onDestroy() {
        super.onDestroy()
        Log.e(TAG,"onDestroy");
    }

    override fun onDetach() {
        super.onDetach()
        Log.e(TAG,"onDetach");
    }

    override fun onAttach(activity: Activity) {
        super.onAttach(activity)
        Log.e(TAG,"onAttach activity");
    }

    override fun onAttach(context: Context) {
        super.onAttach(context)
        Log.e(TAG,"onAttach Context");
    }

    override fun onDestroyView() {
        super.onDestroyView()
    }
}